#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<vector>
#include<set>
#include<algorithm>

using namespace std;

int maxTaskAssign(vector<int>& tasks, vector<int>& workers, int pills, int strength) {
    int n = tasks.size(), m = workers.size();
    sort(tasks.begin(), tasks.end());
    sort(workers.begin(), workers.end());

    auto judge = [&](int mid)->bool {
        int p = pills;
        multiset<int> s;
        for (int i = m - mid; i < m; i++)  s.insert(workers[i]);

        for (int i = mid - 1; i >= 0; i--)
        {
            auto it = prev(s.end());
            if (*it >= tasks[i])
                s.erase(it);
            else
            {
                if (!p)  return false;
                auto rep = s.lower_bound(tasks[i] - strength);
                if (rep == s.end())  return false;
                p--;
                s.erase(rep);
            }
        }
        return true;
    };

    int l = 0, r = min(n, m);
    while (l < r)
    {
        int mid = l + r + 1 >> 1;
        if (judge(mid))  l = mid;
        else r = mid - 1;
    }
    return l;
}